//Copyright (c) 2010, Goodwine & Tibia ML
//Licensed under new BSD License.

// Add the option to be notified about your stamina from Tibia ML
chrome.extension.sendRequest({action : 'loadJQuery'}, function(data) {
	if(data != null) {
		eval(data);
		// Programatically inject inputs and a script.
		var premmy = document.createElement('input');
		premmy.style.marginRight = '5px';
		premmy.type = 'checkbox';
		premmy.id = 'stamina_premmy';
		var label = document.createElement('label');
		label.style.float = 'none';
		label.style.display = 'inline';
		$(label).attr('for', 'stamina_premmy');
		label.innerHTML = 'premmy?';
		var calcButton = $('input[type*=submit][value*=Calculate]')[0];
		var addButton = document.createElement('input');
		addButton.type = 'button';
		addButton.value = 'Notify Me!';
		// Send request to add the stamina notification.
		addButton.onclick = function () {
			var hours = !isNaN(parseInt($('form [name*=hours]')[0].value)) ? parseInt($('form [name*=hours]')[0].value) : -1;
			var minutes = !isNaN(parseInt($('form [name*=minutes]')[0].value)) ? parseInt($('form [name*=minutes]')[0].value) : -1;
			var premmy = $('#stamina_premmy')[0].checked;
			if(hours >= 0 && hours < 42 && minutes >= 0 && minutes < 60)
			chrome.extension.sendRequest({'action' : 'saveStamina' , 'hours':hours, 'minutes':minutes, 'premmy':premmy},function(stamina) {
				console.log('hours: ' + stamina.hours + ' mins: ' + stamina.minutes);
			});
		};
		$('form [name*=hours]')[0].parentElement.appendChild(premmy);
		$('form [name*=hours]')[0].parentElement.appendChild(label);
		calcButton.parentElement.appendChild(addButton);
	}
	else
		console.warn('StaminaML: Failed to load jQuery');
});